package com.sc.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.admin.common.ResponseResult;
import com.sc.admin.pojo.Menu;
import com.sc.admin.pojo.Role;
import com.sc.admin.pojo.RoleMenuPriv;
import com.sc.admin.service.MenuService;
import com.sc.admin.service.RoleMenuPrivService;
import com.sc.admin.service.RoleService;
import com.sc.admin.utils.EasyuiTreeNode;

@Controller
@RequestMapping("/priv/role")
public class RolePrivController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleMenuPrivService rmps;
	@Autowired
	private MenuService menuService;

	@RequestMapping()
	public String privPage() {
		return "priv-role";
	}

	@RequestMapping("/list")
	public @ResponseBody Object list(int page, int rows) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", roleService.getCount());
		List<Role> roles = roleService.getRoles(page, rows);
		map.put("rows", roles);
		return map;
	}

	// 同步加载菜单权限
	@RequestMapping("/menu/tree")
	public @ResponseBody Object getMenuByPid(@RequestParam(defaultValue = "0") int id, int roleId) {
		List<Menu> menus = menuService.getSyncChildrenByPid(id);

		List<EasyuiTreeNode> nodes = new ArrayList<EasyuiTreeNode>();
		// 对象转换
		for (Menu m : menus) {
			EasyuiTreeNode node = ToEasyuiTreeNode(m);
			nodes.add(node);
		}

		// 进行权限判断
		List<RoleMenuPriv> mPrivs = rmps.getRoleMenuPrivByRoleId(roleId);
		for (RoleMenuPriv rmp : mPrivs) {
			int menuId = rmp.getMenuId();
			for (EasyuiTreeNode node : nodes) {
				EasyuiTreeNode find = findEasyuiTreeNodeById(menuId, node);
				if (find != null && find.getChildren().size()==0) {
					find.setChecked(true);
					break;
				}
			}
		}
		return nodes;
	}

	@RequestMapping("/menu/add")
	public @ResponseBody Object addMenuPriv(int roleId, String ids) {
		rmps.addRoleMenuPriv(roleId, ids);
		return new ResponseResult().ok();
	}
	
	// 递归转换同步菜单节点数据
	private EasyuiTreeNode ToEasyuiTreeNode(Menu menu) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(menu.getId());
		node.setText(menu.getName());

		// 判断是否叶子节点
		if (!menu.getIsParent()) {
			node.getAttributes().put("url", menu.getUrl());
			node.setState("open");
		} else {
			List<EasyuiTreeNode> nodes = node.getChildren();
			// 添加children
			List<Menu> children = menu.getChildren();
			for (Menu child : children) {
				EasyuiTreeNode n = ToEasyuiTreeNode(child);
				nodes.add(n);
			}
		}
		return node;
	}

	// 递归查找指定的节点
	private EasyuiTreeNode findEasyuiTreeNodeById(int id, EasyuiTreeNode node) {
		EasyuiTreeNode find = null;
		if (node.getId() == id) {
			find = node;
		} else {
			// 如果是父节点
			if (node.getChildren().size()>0) {
				List<EasyuiTreeNode> children = node.getChildren();
				for (EasyuiTreeNode child : children) {
					find = findEasyuiTreeNodeById(id, child);
					// 找到就跳出当前循环
					if (find != null) {
						break;
					}
				}
			}
		}
		return find;
	}
}
